package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Role;
import com.hopu.service.RoleService;
import com.hopu.util.ResultUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("myjs")
@RestController
public class RoleController {

    @Resource
    private RoleService roleServiceImpl;

    /**
     * 查询下拉框角色列表
     * @return
     */
    @PostMapping("findAllJsName")
    private ResultUtils findAllJsName(){
      List<Role> list = roleServiceImpl.findAllJsName();
      if (!CollectionUtils.isEmpty(list)){
          return new ResultUtils(200,"SUCCESS",list);
      }
      return new ResultUtils(400,"ERROR",null);
    }

    /**
     * 分页展示数据
     * @param pn
     * @return
     */
    @PostMapping("findAllJs")
    private ResultUtils findAllJs(@RequestParam(value = "pn",defaultValue = "1")Integer pn){
        PageHelper.startPage(pn,14);
        List<Role> list = roleServiceImpl.findAllJs();
        PageInfo page = new PageInfo(list,5);
        return  new ResultUtils(200,"SUCCESS",page);
    }

    /**
     * 添加岗位
     * @param jname
     * @return
     */
    @PostMapping("addJs")
    private ResultUtils addJs(@RequestParam(value = "jname",required = true)String jname){
        int num = roleServiceImpl.addJs(jname);
        if (num>0){
            return  new ResultUtils(200,"SUCCESS",null);
        }
        return  new ResultUtils(400,"ERROR",null);
    }

    /**
     * 根据id或者name查找岗位名称
     * @param jname
     * @param jid
     * @return
     */
    @PostMapping("findJs")
    private ResultUtils findJsByNameOrId(@RequestParam(name ="jname",required = false) String jname,
                                         @RequestParam(name = "jid",required = false) Integer jid){
        System.out.println(jname+"==="+jid);
        Role role = roleServiceImpl.findJsByNameOrId(jname,jid);
        if (!ObjectUtils.isEmpty(role)) {
            return new ResultUtils(200,"SUCCESS",role);
        }
        return new ResultUtils(400,"ERROR",role);
    }

    /**
     * 修改
     * @param jname
     * @param jid
     * @return
     */
    @PostMapping("updateJs")
    private ResultUtils updateJs(@RequestParam(value = "jname",required = true)String jname,
                                 @RequestParam(value = "jid",required = true)Integer jid){
        int num = roleServiceImpl.updateJs(jname,jid);
        if (num>0){
            return  new ResultUtils(200,"SUCCESS",null);
        }
        return  new ResultUtils(400,"ERROR",null);
    }
}
